<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="../base_top.jsp" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>新增收货地址</title>
    
	<%@ include file="../base_head.jsp" %>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">    
	<link rel="stylesheet" type="text/css" href="<%=basePath%>js/user/select2.css">
	<script type="text/javascript" src="<%=basePath%>js/jquery-1.12.3.js"></script>
	<script type="text/javascript" src="<%=basePath%>js/user/select2.js"></script>
	<script type="text/javascript" src="<%=basePath%>js/user/area.js"></script>
	<script type="text/javascript" src="<%=basePath%>js/user/location.js"></script>
	<script type="text/javascript" src="<%=basePath%>js/user/select2_locale_zh-CN.js"></script>
	
	<script type="text/javascript">
	function validate() {
		var required = new Array("name", "province", "city", "town", "address", "phone", "postcode");
		for (var i = 0; i<required.length; i++) {
			var value = $("#"+required[i]+"").val().trim();
			if (value == "") {
				var msg = "";
				if (i == 0) {
					msg = "请填写收件人";
				} else if (i == 1 || i == 2 || i == 3) {
					msg = "请选择地区";
				} else if (i == 4) {
					msg = "请填写详细地址";
				} else if (i == 5) {
					msg = "请填写正确手机号码";
				} else if (i == 6) {
					msg = "请填写邮政编码";
				} else {
					return false;
				}
				$("#"+required[i]+"tip"+"").text(msg);
				return false;
			}
		}
		return true;
	}
	function beforeSubmit() {
		var province = $('#loc_province').select2('data').text;
		var city = $('#loc_city').select2('data').text;
		var town = $('#loc_town').select2('data').text;
		if (province != "省份") {
			$("#province").val(province);
		}
		if (city != "地级市") {
			$("#city").val(city);
		}
		if (town != "市、县、区") {
			$("#town").val(town);
		}
	}
	$(function() {
		$("#sub_btn").click(function() {
			beforeSubmit();
			if (validate()) {
				$.ajax({
					cache: false,
					url:"user/addressAdd",
					type: "GET",
					data: $("#form").serialize(),
					dataType:"json",
				    success: function(data) {
				    	if (true) {
				    		alert(data);
				    		window.parent.location.reload(); //刷新父页面
				    		var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
				    		parent.layer.close(index);  // 关闭layer
				    	}
				    }
				});
			}
		});
	});
	</script>
  </head>
  
  <body>
    <form id="form" action="user/addressAdd" method="post">
    <input type="hidden" name="address.addressNum" value='<s:property value="#request.address.addressNum"/>'/>
    <input type="hidden" name="address.ifdefault" value='<s:property value="#request.address.ifdefault"/>'/>
    <input type="hidden" name="address.createTime" value='<s:property value="#request.address.createTime"/>'/>
    <table border=1>
    	<tr><td>收货人：</td></tr>
    	<tr><td><input type="text" id="name" name="address.daName" value='<s:property value="#request.address.daName"/>'/></td><td><label id="nametip"></label></td></tr>
    	<tr><td>所在地区：</td></tr>
    	<tr><td>
    	<input type="hidden" name="province" id="province" value=""/>
    	<input type="hidden" name="city" id="city" value=""/>
    	<input type="hidden" name="town" id="town" value=""/>
    	<select id="loc_province" style="width:120px;"></select>
		<select id="loc_city" style="width:120px; margin-left: 10px"></select>
		<select id="loc_town" style="width:120px;margin-left: 10px"></select>
		</td><td><label id="provincetip"></label><label id="citytip"></label><label id="towntip"></label></td></tr>
		
		<tr><td>详细地址： </td></tr>
		<tr><td><input type="text" id="address" name="address.daAddress" value='<s:property value="#request.address.daAddress"/>'/></td><td><label id="addresstip"></label></td></tr>
    
    	<tr><td>手机号码：</td></tr>
    	<tr><td><input type="text" id="phone" name="address.daPhonenum" value='<s:property value="#request.address.daPhonenum"/>'/></td><td><label id="phonetip"></label></td></tr>
    	
    	<tr><td>固定电话：</td></tr>
    	<tr><td><input type="text" id="fixphone" name="address.daFixphonenum" value='<s:property value="#request.address.daFixphonenum"/>'/></td>
    	
    	<tr><td>邮箱： </td></tr>
    	<tr><td><input type="text" id="email" name="address.daEmail" value='<s:property value="#request.address.daEmail"/>'/></td></tr>
    	
    	<tr><td>邮政编码</td></tr>
    	<tr><td><input type="text" id="postcode" name="address.daPostcode" value='<s:property value="#request.address.daPostcode"/>'/></td><td><label id="postcodetip"></label></td></tr>

		<tr><td>地址名称</td></tr>
		<tr><td><input type="text" id="" name="address.addressName" value='<s:property value="#request.address.addressName"/>'/></td></tr>
		
    	<tr><td><input type="button" id="sub_btn" value="保存收货地址"/></td></tr>
    	
    </table>
    </form>
    
    
  </body>
</html>
